package code.example.base.data.string;

import code.example.tool.LogFactory;

public class Compare {

    public int  compare(String a,String b ){
        int aLength = a.length();
        int blength = b.length();
        int length = aLength < blength ? aLength : blength;
        char[] aArray = a.toCharArray();
        char [] bArray =b.toCharArray();
        for(int i=0;i<length;i++){
            if(aArray[i] < bArray [i]){
                return -1;
            }else if(aArray [i] > bArray[i]){
                return 1;
            }
        }
        return 0;
    }
     public int  index (String a,String b ){
        char [] aArray = a.toCharArray();
        char [] bArray = b.toCharArray();
        int same = findSame(bArray);
        for(int i=0;i<aArray.length;i++){
            int result = compare(aArray,bArray,i,same);
            if(result == -1){
                return i;
            }else {
                i+=result;
            }
        }
        return -1;
     }

     int findSame(char [] bArray){
        char  w = bArray[0];
        for(int i=1;i<bArray.length;i++){
            if(w == bArray[i]){
                return i;
            }
        }
        return -1;
     }

     public int  compare(char [] aArray,char [] bArray,int x, int same){
        for(int i=0;i<bArray.length;i++){
            if(aArray [x+i] != bArray [i]){
                if(i >0 ){
                    if(same >0 && i>same){
                        return x+same;
                    }else{
                        return x+i;
                    }
                }else{
                    return i;
                }
            }
        }
        return -1;
     }

    public static void main(String [] args){
        String a ="abcdefbcdfghij";
        String b = "bcdf";
        Compare compare = new Compare();
        int index = compare.index(a,b);
        LogFactory.getLog().info("a="+a+", b="+b+",index = "+index);
    }
}
